<?php

class DBSchema extends MySqlDatabase {

    public function __construct() {
        $this->host = DBHostName;
        $this->database = DBName;
        $this->user = DBUserName;
        $this->password = DBPassword;
    }

    private function GetReferenceTables() {
        $tTableDBName = Tables_in_ . $this->database;
        $tTables = array();


        $tSQL = 'SHOW FULL TABLES WHERE Table_type !=  "view" AND ( ' . $tTableDBName . ' LIKE "%ref%" AND ' . $tTableDBName . ' NOT LIKE "%user%"  AND ' . $tTableDBName . ' NOT LIKE "%person_kategori%" )';

        //PERIKSA: koneksi dengan database. Reconnect DB		
        if (!$this->IsOpen()) {
            //Lakukan Reconnect jika Status Koneksi = 0
            $this->ConnectDB();
        }
        $tResult = $this->Execute($tSQL);


        $i = 0;
        while ($tResult->MoveNext()) {
            $tTables[$i] = $tResult->$tTableDBName;
            
            //if($i>2) break;
            $i++;
        }

        /*if (CORE_DEVELOPING_MODE) {
            echo '<div class="Debug"><b>function: GetReferenceTables()</b><br/>' . $tSQL . '</div>';
        }*/
        return $tTables;
    }
    
    
    private function GetTableReferenceOfReferenceTables(){
        $tTables = array();

        $tSQL = 'SELECT ke.referenced_table_name parent, ke.table_name child, column_name as child_field, referenced_column_name as parent_field, ke.constraint_name FROM information_schema.KEY_COLUMN_USAGE ke WHERE table_schema="'.$this->database.'" AND ke.referenced_table_name IS NOT NULL AND  ke.table_name LIKE "%ref%" AND ke.table_name NOT LIKE "%user%" AND ke.table_name NOT LIKE "%person_kategori%" ORDER BY ke.table_name';
        /*if (CORE_DEVELOPING_MODE) {
            echo '<div class="Debug"><b>function: GetTableReferenceOfReferenceTables()</b><br/>' . $tSQL . '</div>';
        }*/
        //PERIKSA: koneksi dengan database. Reconnect DB		
        if (!$this->IsOpen()) {
            //Lakukan Reconnect jika Status Koneksi = 0
            $this->ConnectDB();
        }
        $tResult = $this->Execute($tSQL);


        $i = 0; $tChild= ""; $tChildIndex=0;
        while ($tResult->MoveNext()) {
            if($tChild == $tResult->child){
                $tChildIndex++;
            } else {
                $tChild= $tResult->child;
                $tChildIndex=0;
            }
            $tTables[$tResult->child][$tChildIndex][ParentTable] = $tResult->parent;
            $tTables[$tResult->child][$tChildIndex][ParentField] = $tResult->parent_field;
            $tTables[$tResult->child][$tChildIndex][ChildField] = $tResult->child_field;
            
            $i++;
        }

        return $tTables;
    }

    private function ConvertNameToAlias($pName) {
        $tAlias= $pName;
        //echo $pName.' -> ';
        $tAlias= str_replace("app", "", $tAlias);
        $tAlias= str_replace("hrs", "", $tAlias);
        $tAlias= str_replace("donatur", "", $tAlias);
        //echo $tAlias.' -> ';
        $tAlias= str_replace("_ref_", "", $tAlias);
        //echo $tAlias.' -> ';
        //$tAlias= str_replace("_", " ", $tAlias);
        //echo $tAlias.' -> ';
        
        //$tAlias= ucwords($tAlias);
        //echo $tAlias.'<br>';
        return $tAlias;
    }

    private function GetAllDataFromTableReferenceOfReferenceTables() {
        
        $tTables = $this->GetReferenceTables();
    }
    
    private function GetAllDataFromReferenceTables() {
        $tTables = $this->GetReferenceTables();

        if (count($tTables) > 0) {
            //PERIKSA: koneksi dengan database. Reconnect DB		
            if (!$this->IsOpen()) {
                //Lakukan Reconnect jika Status Koneksi = 0
                $this->ConnectDB();
            }

            foreach ($tTables as $tTableName) {
                $tSQL = 'SELECT * FROM ' . $tTableName . ' ';
                $tResult = $this->Execute($tSQL);

                $tAliasTableName= $this->ConvertNameToAlias($tTableName);
                
                $tRow = 0;
                while ($tResult->MoveNext()) {
                    $tRecord = $tResult->GetRecord($tRow);
                    

                    foreach ($tRecord as $tFieldName => $tValue) {
                        if (!is_numeric($tFieldName)) {
                            $tAliasFieldName= $this->ConvertNameToAlias($tFieldName);
                            $tReferenceTables[$tAliasTableName][$tRow][$tAliasFieldName] = $tValue;
                            /*$tTemp= (object) array (
                                $tAliasTableName=> (object) array( 
                                    $tFieldName=>$tValue
                                )
                            );
                            $tReferenceTables->$tAliasTableName= (object) array();
                            $tTemp->$tAliasTableName= (object) array( 
                                    $tFieldName=>$tValue);
                            
                            array_push($tReferenceTables, $tTemp);*/
                        }
                    }
                    $tRow++;
                }
            }
        } else {
            echo 'no result';
        }

        /*if (CORE_DEVELOPING_MODE) {
            $no = 1;
            echo '<div class="Debug">';
            foreach ($tTables as $tTableName) {
                echo $no . ': ' . $tTableName . '<br>';
                $no++;
            }
            echo '</div>';
        }*/
        return $tReferenceTables;
    }
    

    public function GetJSONAllDataFromReferenceTables() {
        $tJSONRep = new JSONDataRepresentation();
        $tData = $this->GetAllDataFromReferenceTables();
        
        print_r(json_encode($tData) ); //echo '<hr>';
        
        /*
        $tRefTables= $this->GetTableReferenceOfReferenceTables();
        foreach ($tRefTables as $tChildTable => $tRefs) {
            $tConfig= array();
            $tAliasChildTable= $this->ConvertNameToAlias($tChildTable);
            
            foreach($tRefs as $tIndex=>$tValue) {
                $tAliasParentTable= $this->ConvertNameToAlias($tValue['ParentTable']);
                $tAliasParentField= $this->ConvertNameToAlias($tValue['ParentField']);
                $tAliasChildTable= $this->ConvertNameToAlias($tValue['ChildField']);
                
                $tConfig[$tAliasChildTable][$tIndex]["Parent"]= $tAliasParentTable;
                $tConfig[$tAliasChildTable][$tIndex]["Parent ID"]= $tAliasParentField;
                $tConfig[$tAliasChildTable][$tIndex]["Child ID"]= $tAliasChildTable;
            }
            $tJSONRep->InsertConfig("RefTables", $tConfig);
        }
        
        foreach ($tData as $tTableName => $tData) {
            $tJSONRep->CreateNewData();
            $tJSONRep->InsertData($tTableName, $tData);
        }
        $tResult = $tJSONRep->PrintJSONDataRepresentation();*/
    }

}

?>
